import fs from '@ohos.file.fs';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import db from '../common/core/db';
import utils from '../common/core/utils';
import current from '../common/core/current';
import UIAbility from '@ohos.app.ability.UIAbility';
import emoteClient from '../common/http/emoteClient';


export default class EntryAbility extends UIAbility {

  onCreate(want, launchParam) {
    window.getLastWindow(this.context).then(lastWindow => {
      lastWindow.on('windowSizeChange',data => {
        utils.setWindowRect(data.width, data.height)
      })
    })
    globalThis.abilityContext = this.context;
    globalThis.filesDir = this.context.filesDir;
    let root = `${this.context.filesDir}/${this.context.applicationInfo.name}`;
    fs.access(root).then((res) => {
      if (!res)
        fs.mkdir(root);
    });
  }

  onDestroy() {
    window.getLastWindow(this.context).then(lastWindow => {
      lastWindow.off('windowSizeChange')
    })
    db.instance.update('first', true);
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    utils.setLayoutFullScreen(windowStage, '#ffffff', '#000000', '#f7f7f7', '#000000');
    db.createInstance(this.context);
    current.instance.loadInfo();
    emoteClient.loadCache();
    windowStage.loadContent('pages/Index');
  }

  onWindowStageDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }


}